<?php

class WP_Widget_Recommend extends WP_Widget{	
	function __construct(){
		$widget_ops=array('classname'=>'tjyd layout5','description'=>"最新推荐");
		parent::__construct(false, "最新推荐", $widget_ops);
	}
	
	function widget($args,$instance){
		extract($args);
		$title=apply_filters('widget_title', '最新推荐', $instance, $this->id_base);
		$ids=!empty($instance['ids'])?$instance['ids']:'0';
		echo $before_widget;
		if($title){
			echo $before_title.$title.$after_title;
		}
		//内容
		self::getHtml("showPosts=$ids");
		echo $after_widget;
	}
	
	function update($new_instance,$old_instance){
		$instance = $old_instance;
		$instance['ids'] = strip_tags(stripslashes($new_instance['ids']));;
		return $instance;
	}
	
	function form($instance){
		$instance = wp_parse_args((array)$instance,array('ids'=>'1,2,3,4,5,6'));
		$ids= strip_tags($instance['ids']);
		?>
		<p><label for="<?php echo $this->get_field_id('ids'); ?>">文章编号</label> <input class="widefat" id="<?php echo $this->get_field_id('ids'); ?>" name="<?php echo $this->get_field_name('ids'); ?>" type="text" value="<?php echo $ids; ?>" /></p>
	<?php 
	}
	
	function getHtml($args=''){		
		$default=array('ids'=>"217,215,37,35");
		$parse_args=wp_parse_args($args,$default);
		extract($parse_args);
		$args_post = array(
			'orderby' => post_date,
		    'posts_per_page' =>16,
		    'post__in'=>explode(",", $parse_args['ids']),
		    'ignore_sticky_posts'=>1
		);
		$post_query = new WP_Query($args_post);
		if($post_query->have_posts()):
			echo '<div class="bd"><ul class="mt15">';
			while($post_query->have_posts()):
				$post_query->the_post();
				echo '<li>';
				echo '<a class="pic">'.getFirstImage(get_the_ID(),140,100).'</a>';
                echo '<p><a href="'.the_permalink().'">'.get_the_title().'</a></p>';
                echo '</li>';
			endwhile;
			echo '</ul></div>';
			
		 endif;		
		wp_reset_query();
	}
}
?>